/*
 ============================================================================
 Name        : main.c
 Author      : dogguo
 Time        : 2016/05/01 23:59
 Version     : V0.0.1
 Copyright   : WifiShare Cooperation
 Description : WL APP
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <ctype.h>
#include <stdarg.h>


#include "task.h"
#include "lib/configini/configini.h"
#include "app/gconstant.h"
#include "task_serv.h"
#include "app/gvar.h"

#define CONF_PATH_DEF "conf/def.conf"
#define CONF_PATH_USER "conf/user.conf"
#include "lib/cjson/cJSON.h"


int api_task_conf_vpncrt() {
    printf("api_task_conf_vpncrt <<<--- \n");

    // if find not exsit vpncrt files or they were invalid ....................................

    // get server vpcrt url
    char* vpncrt_url = (char*)malloc(sizeof(char*) * 128);
    if(!get_vpncrt_url(vpncrt_url, "&deviceId=008")) {
        printf("api_task_conf_vpncrt <<<--- get_vpncrt_url failed ! \n");
        return 0;
    }
    printf("api_task_conf_vpncrt <<<--- get_vpncrt_url success !!!!! vpncrt_url =  %s \n", vpncrt_url);

    // download vpcrt files by url then write the compress file
    if(!net_http_download(vpncrt_url, PATH_LOCAL_VPNCRT)) {
        printf("api_task_conf_vpncrt <<<--- net_http_download failed ! \n");
        return 0;
    }
    printf("api_task_conf_vpncrt <<<--- net_http_download success !!!!! PATH_LOCAL_VPNCRT =  %s \n", PATH_LOCAL_VPNCRT);

    char* file_name = basename(vpncrt_url);
    printf("api_task_conf_vpncrt <<<--- file_name =  %s \n", file_name);
    char* tar_to_path = " /home/meng/work/wifishare/prj/wl_app/res/file/vpncrt";
    char cmd_line[128];
    strcpy(cmd_line, "bash sh/extract_vpncrt.sh ");
    strcat(cmd_line, file_name);
    strcat(cmd_line, tar_to_path);



    printf("api_task_conf_vpncrt <<<--- cmd_line = %s \n", cmd_line);

    // uncompress file to specify path ...................
    if(!system_sh(cmd_line)) {
        printf("api_task_conf_vpncrt <<<--- system_sh extract_vpncrt.sh failed ! \n");
        return 0;
    }
    printf("api_task_conf_vpncrt <<<--- system_sh extract_vpncrt.sh success ! \n");

    // restart openvpn process ................... OK !!!!!!!!!!
    if(!system_sh("sh/restart_vpnd.sh")) {
        printf("api_task_conf_vpncrt <<<--- system_sh restart_vpnd.sh failed ! \n");
        return 0;
    }
    printf("api_task_conf_vpncrt <<<--- system_sh restart_vpnd.sh success ! \n");

    printf("api_task_conf_vpncrt --->>> \n");
    return 1;
}


/*  */
int api_task_post_wifist() {
    printf("api_task_post_wifist <<<--- \n");

    cJSON* root_cj = NULL;
    root_cj = cJSON_CreateObject();
    if(NULL == root_cj) {
        printf("cJSON_CreateObject failed \n");
        return NULL;
    }
    cJSON* sub_cj = cJSON_CreateArray();
    if(NULL == sub_cj) {
        cJSON_Delete(root_cj);
        return NULL;
    }
    int i = 0;
    for(; i < 6; i++) {
        if(i == 0) {
            cJSON* s_sub_cj = cJSON_CreateObject();
            if(NULL == s_sub_cj) {
                cJSON_Delete(root_cj);
                return NULL;
            }
            cJSON_AddNumberToObject(s_sub_cj, "wifiId", 2);
            cJSON_AddNumberToObject(s_sub_cj, "invalidType", 1);
            cJSON_AddItemToArray(sub_cj, s_sub_cj);
        }
        if(i == 1) {
            cJSON* s_sub_cj = cJSON_CreateObject();
            if(NULL == s_sub_cj) {
                cJSON_Delete(root_cj);
                return NULL;
            }
            cJSON_AddNumberToObject(s_sub_cj, "wifiId", 3);
            cJSON_AddNumberToObject(s_sub_cj, "invalidType", 2);
            cJSON_AddItemToArray(sub_cj, s_sub_cj);
        }
    }
    cJSON_AddItemToObject(root_cj, "wifiList", sub_cj);

    char* param_val = cJSON_Print(root_cj);
    if(NULL == param_val) {
        cJSON_Delete(root_cj);
        return NULL;
    }
    printf("api_task_conf_vpncrt <<<--- param_val = %s \n", param_val);

    char* param_key  = "&wifiList=";
    int param_key_len = strlen(param_key);
    int param_val_len = strlen(param_val);

    char* param = (char*) malloc(param_key_len + param_val_len);

    arr_char_pcpy(param, 0, param_key, param_key_len);
    arr_char_pcpy(param, param_key_len, param_val, param_val_len + 1);
    printf("api_task_conf_vpncrt <<<--- param = %s \n", param);

    if(!post_wifi_status(param)) {
        printf("api_task_conf_vpncrt <<<--- post_wifi_status failed ! \n");
        return 0;
    }
    printf("api_task_conf_vpncrt <<<--- post_wifi_status success !!!!! \n");

    cJSON_Delete(root_cj);

    printf("api_task_post_wifist --->>> \n");
    return 1;
}


/* */
int api_task_post_devicegpm() {
    printf("api_task_post_devicegpm <<<--- \n");

    cJSON* root_cj = NULL;
    root_cj = cJSON_CreateObject();
    if(NULL == root_cj) {
        printf("cJSON_CreateObject failed \n");
        return NULL;
    }
    cJSON* sub_cj = cJSON_CreateArray();
    if(NULL == sub_cj) {
        cJSON_Delete(root_cj);
        return NULL;
    }
    int i = 0;
    for(; i < 6; i++) {
        if(i == 0) {
            cJSON* s_sub_cj = cJSON_CreateObject();
            if(NULL == s_sub_cj) {
                cJSON_Delete(root_cj);
                return NULL;
            }
            cJSON_AddNumberToObject(s_sub_cj, "wifiId", 3);
            cJSON_AddNumberToObject(s_sub_cj, "wifiUserId", 2);
            cJSON_AddNumberToObject(s_sub_cj, "wifiGpm", 131434);
            cJSON_AddItemToArray(sub_cj, s_sub_cj);
        }
        if(i == 1) {
            cJSON* s_sub_cj = cJSON_CreateObject();
            if(NULL == s_sub_cj) {
                cJSON_Delete(root_cj);
                return NULL;
            }
            cJSON_AddNumberToObject(s_sub_cj, "wifiId", 3);
            cJSON_AddNumberToObject(s_sub_cj, "wifiUserId", 2);
            cJSON_AddNumberToObject(s_sub_cj, "wifiGpm", 131434);
            cJSON_AddItemToArray(sub_cj, s_sub_cj);
        }
    }

    cJSON_AddNumberToObject(root_cj, "deviceId", 8);
    cJSON_AddNumberToObject(root_cj, "deviceUserId", 5);
    cJSON_AddItemToObject(root_cj, "gpmInfo", sub_cj);

    char* param_val = cJSON_Print(root_cj);
    if(NULL == param_val) {
        cJSON_Delete(root_cj);
        return NULL;
    }

    char* param_key  = "&gpmList=";
    printf("api_task_post_devicegpm <<<--- param_key = %s \n", param_key);
    printf("api_task_post_devicegpm <<<--- param_val = %s \n", param_val);
    int param_key_len = strlen(param_key);
    int param_val_len = strlen(param_val);

    char* param = (char*) malloc(param_key_len + param_val_len);

    arr_char_pcpy(param, 0, param_key, param_key_len);
    arr_char_pcpy(param, param_key_len, param_val, param_val_len + 1);
    printf("api_task_post_devicegpm <<<--- param = %s \n", param);

    if(!post_device_gpm(param)) {
        printf("api_task_post_devicegpm <<<--- post_device_gpm failed ! \n");
        return 0;
    }
    printf("api_task_post_devicegpm <<<--- post_device_gpm success !!!!! \n");

    cJSON_Delete(root_cj);

    printf("api_task_post_devicegpm --->>> \n");
    return 1;
}


